package com.guigu.controller;

import com.guigu.entity.OrdersettingBean;
import com.guigu.service.OrdersettingService;
import com.guigu.vo.OrdersettingVO;
import com.guigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/findAll")
    public Result<List<OrdersettingBean>> findAll() {

        List<OrdersettingBean> OrdersettingBeanList = this.ordersettingService.selectOrdersetting();

        System.out.println("预约设置结果集："+OrdersettingBeanList);

        return new Result<>("200", "true", "成功查询", OrdersettingBeanList);
    }

    @PostMapping("/tjrlyy")
    public Result tjrlyy(@RequestBody OrdersettingVO ordersettingVO ) {
        // 处理 POST 请求的逻辑代码

        System.out.println("ordersettingVO对象：" + ordersettingVO);
        System.out.println("ordersettingVO日期：" + ordersettingVO.getOrderDate());

        // 将日期字符串转换为Date类型
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date zhorderDate = null;
        try {
            zhorderDate = dateFormat.parse(ordersettingVO.getOrderDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("转换之后的日期：" + zhorderDate);

        OrdersettingBean ordersettingBean=new OrdersettingBean(
                ordersettingVO.getId(),
                zhorderDate,
                ordersettingVO.getNumber(),
                ordersettingVO.getReservations()

        );

        OrdersettingBean cxdyysz=this.ordersettingService.selectOrdersettingByorderDate(ordersettingBean);

        System.out.println("查询的预约设置：" + cxdyysz); // 输出格式化后的日期时间

        if(cxdyysz==null){

            //this.ordersettingService.addordersetting(ordersettingBean);

        }else{

            this.ordersettingService.updateOrdersetting(ordersettingBean);
        }



        return new Result<>("200", "true", "操作成功", "操作成功");
    }
}
